<?php
class ListAdminForm extends Form{
	function ListAdminForm(){
		Form::Form('ListAdminForm');
	}
	
	function draw(){
		$this->beginForm();	
		global $display;	
		
		$acc_groups	 =CGlobal::$group;
		$user_permits=array();
		$all_users	 =array();
		$allist_users=array();
		$gids 		 = '';
		
		foreach ($acc_groups as $group)
		{
			$acc_groups[$group['id']]['users']	=array();
			$acc_groups[$group['id']]['pids']	=array();
			$acc_groups[$group['id']]['pid_str']='';
			
			if(!in_array($group['id'],array(1,9)))
			$gids.=($gids?',':'').$group['id'];
		}
		
		$re = DB::query("SELECT * FROM user_groups");
		
		if($re)
		{
			while($group = mysql_fetch_assoc($re))
			{
				if(!in_array($group['id'],array(1,9)))
				{
					CGlobal::$group[$group['id']] = $group;
					
					$group['users']		=	array();
					$group['pids']		=	array();
					$group['pid_str']	=	'';
					
					$group['del_link']	=	Url::build_current(array('cmd'=>'del_group','id'=>$group['id']));;
					$group['edit_link']	=	Url::build_current(array('cmd'=>'edit_group','id'=>$group['id']));;
				
					$gids	.=	($gids?',':'')	.	$group['id'];
					
					$acc_groups[$group['id']] = $group;
				}
			}
		}
		
		ECLib::get_config();
		
		if(isset(CGlobal::$configs['site_permission']))
		{
			CGlobal::$permit = unserialize(CGlobal::$configs['site_permission']['cf_value']);
			
			foreach (CGlobal::$permit as $permit)
			{
				if($permit && is_array($permit))
				{
					foreach ($permit as $pm=>$des)
					{
						CGlobal::$all_permit[$pm] = $des;
					}
				}
			}
		}
		
		if($gids)
		{
			//$sql = "SELECT pids,ref_id FROM user_permit WHERE ref_id IN(".$gids.") AND type=0";//lấy quyền của nhóm
			$sql = "SELECT type, ref_id, pids FROM user_permit";//lấy quyền của nhóm
			$result = DB::query($sql);		
			
			if($result)
			{
				while($row = mysql_fetch_assoc($result))
				{
					if($row['type']==0)//group
					{
						$acc_groups[$row['ref_id']]['pid_str'].=($acc_groups[$row['ref_id']]['pid_str']?'|':'').$row['pids'];
						
						$acc_groups[$row['ref_id']]['pids']=array();
						
						if($row['pids'])
						{
							$pids = explode('|',$row['pids']);
							
							if($pids)
							{
								foreach ($pids as $pid)
								{
									if($pid && isset(CGlobal::$all_permit[$pid]))
									{
										$acc_groups[$row['ref_id']]['pids'][$pid] = CGlobal::$all_permit[$pid];
									}
								}
							}
						}
					}
					else//user
					{
						$user_permits[$row['ref_id']]	=$row['pids'];
					}
				}
			}
		}
		
		
		############################################################################################################
		$sql = 'SELECT id, email, full_name, gids FROM user AS a WHERE a.gids != "0" AND  a.gids != ""';
		
		$result = DB::query($sql);
		
		if($result)
		{
			while($user = mysql_fetch_assoc($result))
			{
				$user['full_name'] 		= 	String::trimSpace($user['full_name']);
				
				$all_users[$user['id']]	=	$user;
				
				$user['pid_str'] 		= isset($user_permits[$user['id']])?$user_permits[$user['id']]:'';
				
				$groups=explode('|',$user['gids']);
				
				foreach ($groups as $gid)
				{
					if(isset($acc_groups[$gid]))
					{
						if(!User::is_root() && $gid==9)
							$user['del_enable'] 	= 0;
						else
							$user['del_enable'] 	= 1;
							
						$acc_groups[$gid]['users'][]=$user;
					}
				}
				
				$pid_str = isset($user_permits[$user['id']]) ? $user_permits[$user['id']] : '';
				
				$pid_arr=explode('|',$pid_str);
				
				$user['pids']=array();
				
				if($pid_arr)
				{
					foreach ($pid_arr as $pid)
					{
						if($pid && isset(CGlobal::$all_permit[$pid]))
						{
							$user['pids'][$pid]=CGlobal::$all_permit[$pid];
						}
					}
				}
				
				if($user['pids'])
				$allist_users[$user['id']]=$user;
			}				
		}
		
		$display->add('acc_groups',$acc_groups);
		############################################################################################################
		
			
		$display->add('all_users',json_encode($all_users));
		$display->add('allist_users',$allist_users);
		
		$all_groups	=	CGlobal::$group;
		
		if(!User::is_root()){
			unset($all_groups[9]);
		}
		
		$display->add('all_groups',json_encode($all_groups));
		
		$display->add('all_permits',json_encode(CGlobal::$permit));

        $display->add('is_root',User::is_root());

		$display->output('ListAdmin');
		$this->endForm();
	}
}
?>